package com.sneaker.shower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sneaker.shower.common.BizException;
import com.sneaker.shower.entity.domain.AppraisedDO;
import com.sneaker.shower.entity.domain.organization.OrganizationDO;
import com.sneaker.shower.entity.dto.AppraisedDTO;
import com.sneaker.shower.mapper.AppraisedMapper;
import com.sneaker.shower.mapstruct.AppraisedMapStruct;
import com.sneaker.shower.service.AppraisedService;
import com.sneaker.shower.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AppraisedServiceImpl extends ServiceImpl<AppraisedMapper, AppraisedDO> implements AppraisedService {
    private OrganizationService organizationService;

    @Autowired
    public AppraisedServiceImpl(OrganizationService organizationService) {
        this.organizationService = organizationService;
    }

    @Transactional
    @Override
    public Boolean add(AppraisedDTO dto) {
        OrganizationDO organizationDO = organizationService.getById(dto.getOrganizationGuid());
        if (organizationDO == null) {
            throw new BizException("店铺不存在");
        }
        AppraisedDO appraisedDO = AppraisedMapStruct.INSTANCE.dto2DO(dto);
        appraisedDO.setGuid(IdWorker.getId());
        return 1 == baseMapper.insert(appraisedDO);
    }

    @Transactional
    @Override
    public Boolean modify(AppraisedDTO dto) {
        OrganizationDO organizationDO = organizationService.getById(dto.getOrganizationGuid());
        if (organizationDO == null) {
            throw new BizException("店铺不存在");
        }
        if (dto.getGuid() == null) {
            throw new BizException("请选择要修改的评价");
        }
        AppraisedDO db = baseMapper.selectById(dto.getGuid());
        if (db == null) {
            throw new BizException("评价不存在");
        }
        AppraisedDO appraisedDO = AppraisedMapStruct.INSTANCE.dto2DO(dto);
        appraisedDO.setGuid(db.getGuid());
        return 1 == baseMapper.updateById(appraisedDO);
    }

    @Override
    public Boolean del(Long guid) {
        return 1 == baseMapper.deleteById(guid);
    }

    @Override
    public List<AppraisedDTO> list(Long orgnaizationGuid, Long productGuid) {
        if (orgnaizationGuid == null) {
            throw new BizException("无法确认当前分店");
        }
        if (productGuid == null) {
            throw new BizException("请选择产品查看评价");
        }
        return baseMapper.selectList(
                new LambdaQueryWrapper<AppraisedDO>()
                        .eq(AppraisedDO::getOrganizationGuid, orgnaizationGuid)
                        .eq(AppraisedDO::getProductGuid, productGuid)
        ).stream().map(AppraisedMapStruct.INSTANCE::do2DTO).collect(Collectors.toList());
    }
}
